/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_DBOpen.
	/// </summary>
	public class Block_DBOpen : Block
	{

		private long _passwordLength;
		private string _dataSource;
		private string _username;
		private string _passwordEncrypted;
		private string _passwordDecrypted;
		private string _passwordStudioEncrypted;
		private string _dbIdString;
		private string _openReadOnly;
		private string _cursorLibrary;
		private string _cursorType;
		private string _cursorConcurrency;
		private string _connectionTimeout;


		public override TYPE GetBlockType()
		{
			return TYPE.DBOpen;
		}

		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_DBOpen::GetXML Started.");	
			// <DbOpen name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<DbOpen name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...

			// ...database="DB_ID"...
			xml += " database=\"" + Utils.XmlString(_dbIdString) + "\"";
			// ...datasource="SOURCE"...
			xml += " datasource=\"" + Utils.XmlString(_dataSource) + "\"";
			// ...user="USER"...
			xml += " user=\"" + Utils.XmlString(_username) + "\"";
			// ...password="PASS"...
			xml += " password=\"" + Utils.XmlString(_passwordStudioEncrypted) + "\"";
			// ...readOnly="READ ONLY"...
			xml += " readOnly=\"" + Utils.XmlString(_openReadOnly) + "\"";
			// ...cursorLib="CURSOR LIB"...
			xml += " cursorLib=\"" + Utils.XmlString(_cursorLibrary) + "\"";
			// ...cursorType="CURSOR TYPE"...
			xml += " cursorType=\"" + Utils.XmlString(_cursorType) + "\"";
			// ...cursorConcurency="CURSOR CONCURRENCY"...
			xml += " cursorConcurency=\"" + Utils.XmlString(_cursorConcurrency) + "\"";
			// ...timeout="CONNECTION TIMEOUT"...
			xml += " timeout=\"" + Utils.XmlString(_connectionTimeout) + "\"";

			// End the header xml-part.
			xml += ">\r\n";

			// <Inputs>
			xml += GetInputsXML(_blockId);
		
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();

			// </DbOpen>
			xml +="\t\t\t</DbOpen>\r\n";
			return xml;
		}


		public Block_DBOpen(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.
			Trace.Debug("Block_DBOpen created.");
			
			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, 0));
			outputErrorIds.Add(new BlockOutput(OUTPUT.ConnectFailed, 31));
			outputErrorIds.Add(new BlockOutput(OUTPUT.SQLFailed, 168));

			_currentPos += 8;

			_passwordLength = rawData[_currentPos + 4] + rawData[_currentPos + 5] * 256;
			// TODO: remove once I figure out passwords...
			Settings.Instance.Unimplemented("Block_DBOpen: Password not reversed.");
			AddToBlockNote("This block '" + _name + "' has a password, this has not been reversed.");

			_currentPos += 32;

			// Data source
			_dataSource = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
			
			// Username
			_username = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// Password
			_passwordEncrypted = Utils.DataToString(rawData, _currentPos, _passwordLength);
			_currentPos += _passwordLength;
			// TODO: Decrypt password
			_passwordDecrypted = "";
			// TODO: Encrypt it again for studio.
			_passwordStudioEncrypted = "";

			// DB id string
			_dbIdString = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// open read only
			_openReadOnly = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// cursor library
			_cursorLibrary = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// cursor type
			_cursorType = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// cursor concurrency
			_cursorConcurrency = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// connection timeout
			_connectionTimeout = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			Trace.Debug("_dataSource: " + _dataSource +
				", _username = " + _username +
				", _passwordDecrypted = " + _passwordDecrypted + 
				", _dbIdString = " + _dbIdString +
				", _openReadOnly = " + _openReadOnly +
				", _cursorLibrary = " + _cursorLibrary +
				", _cursorType = " + _cursorType +
				", _cursorConcurrency = " + _cursorConcurrency +
				", _connectionTimeout = " + _connectionTimeout);
		}
	}
}